//
//  Main5ViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/9/6.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "Main5ViewController.h"
#import "UIColor+Hex.h"
#import "AuthViewController.h"
#import "UserDefaultUtil.h"
#import "MBProgressHUD+MJ.h"
#import "UserDefaultUtil.h"
#import "HttpReauestParam.h"
#import "AFHTTPRequestOperationManager.h"
#import "SystemViewController.h"
#import "UserInfoViewController.h"
#import "SearchOrderViewController.h"
#import "AboutUsViewController.h"
#import "WalletViewController.h"
@interface Main5ViewController ()

@end

@implementation Main5ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];

    _shuLine1.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    _shuLine2.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    
    self.top_image.layer.masksToBounds = YES;
    self.top_image.layer.cornerRadius = 40;
    [self.top_image.layer setBorderWidth:3];
    [self.top_image.layer setBorderColor:[UIColor whiteColor].CGColor];
    
    self.top_View.image = [UIImage imageNamed:@"main5_top_bg"];
    
    [self.bottomView1 setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]]forState:UIControlStateNormal];
    [self.bottomView1 setBackgroundImage:[self createImageWithColor:[UIColor lightGrayColor]]forState:UIControlStateSelected];
    [self.bottomView2 setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]]forState:UIControlStateNormal];
    [self.bottomView2 setBackgroundImage:[self createImageWithColor:[UIColor lightGrayColor]]forState:UIControlStateSelected];
    [self.bottomView3 setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]]forState:UIControlStateNormal];
    [self.bottomView3 setBackgroundImage:[self createImageWithColor:[UIColor lightGrayColor]]forState:UIControlStateSelected];
    [self.bottomView4 setBackgroundImage:[self createImageWithColor:[UIColor whiteColor]]forState:UIControlStateNormal];
    [self.bottomView4 setBackgroundImage:[self createImageWithColor:[UIColor lightGrayColor]]forState:UIControlStateSelected];
    
    [self.bottomView1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.bottomView3 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.bottomView4 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    
}

-(void)setUserView{
    NSString *userImage = [UserDefaultUtil getData:USER_PHOTO];
    NSString *userName = [UserDefaultUtil getData:USER_NAME];
    NSString *user_car_team = [UserDefaultUtil getData:USER_CARTEAM];
    NSString *user_auth = [NSString stringWithFormat:@"%@",[UserDefaultUtil getData:USER_STATUS]];
    
    _user_phone.text = [UserDefaultUtil getData:USER_PHONE];
    if(userImage != nil && ![userImage isEqualToString:@""]){
        [_top_image loadImage:[NSURL URLWithString:userImage]];
        _top_image.layer.cornerRadius = 40;
    }
    if(userName != nil && ![userName isEqualToString:@""]){
        _user_name.text = userName;
    }
    if(user_car_team != nil && ![user_car_team isEqualToString:@""]){
        _user_car_team.text = user_car_team;
    }
    if (user_auth != nil) {
        if([user_auth isEqualToString:@"0"]){
            _user_auth.text = @"已认证";
        }else{
            _user_auth.text = @"未认证";
        }
    }else{
        _user_auth.text = @"未认证";
    }
    
    NSString *lab1 = [NSString stringWithFormat:@"%@",[UserDefaultUtil getData:PEOPLE_LAB1]];
    NSString *lab2 = [NSString stringWithFormat:@"%@",[UserDefaultUtil getData:PEOPLE_LAB2]];
    NSString *lab3 = [NSString stringWithFormat:@"%@",[UserDefaultUtil getData:PEOPLE_LAB3]];
    if (lab1 != nil && ![lab1 isEqualToString:@""]) {
        _lab1.text = [NSString stringWithFormat:@"%@元",lab1];
    }else{
        _lab1.text = [NSString stringWithFormat:@"%@元",@"0.00"];
    }
    if (lab2 != nil && ![lab2 isEqualToString:@""]) {
        _lab2.text = [NSString stringWithFormat:@"%@元",lab2];
    }else{
        _lab2.text = [NSString stringWithFormat:@"%@元",@"0.00"];
    }
    
    if (lab3 != nil && ![lab3 isEqualToString:@""]) {
        _lab3.text = [NSString stringWithFormat:@"%@",lab3];
    }else{
        _lab3.text = [NSString stringWithFormat:@"%@",@"0"];
    }
    
}
- (UIImage *) createImageWithColor: (UIColor *) color
{
    CGRect rect = CGRectMake(0.0f,0.0f,1.0f,1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *myImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return myImage;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)buttonClick:(id)sender {
    NSLog(@"点击");
    NSString *user_auth = [NSString stringWithFormat:@"%@",[UserDefaultUtil getData:USER_STATUS]];
    UIStoryboard *sb= [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
    AuthViewController *authViewController;
    UserInfoViewController *userInfoViewController;
    SystemViewController *systemVc;
    AboutUsViewController *aboutUsVc;
    WalletViewController *walletVc;
    UIButton *button = sender;
    switch (button.tag) {
        case 1:
            walletVc = [sb instantiateViewControllerWithIdentifier:@"wallet_view"];
            [self.navigationController pushViewController:walletVc animated:YES];
            break;
        case 2:
            if (user_auth != nil && [user_auth isEqualToString:@"0"]) {
                userInfoViewController = [sb instantiateViewControllerWithIdentifier:@"userinfo_view"];
                [self.navigationController pushViewController:userInfoViewController animated:YES];
            }else{
                authViewController = [sb instantiateViewControllerWithIdentifier:@"auth_view"];
                authViewController.type = @"1";
                [self.navigationController pushViewController:authViewController animated:YES];
            }
            
            break;
        case 3:

            systemVc = [sb instantiateViewControllerWithIdentifier:@"system_view"];
            [self.navigationController pushViewController:systemVc animated:YES];

            break;
        case 4:
            aboutUsVc = [sb instantiateViewControllerWithIdentifier:@"about_us_view"];
            [self.navigationController pushViewController:aboutUsVc animated:YES];
            break;
            
        default:
            break;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [self setUserView];
    [super viewWillAppear:animated];
    
}
-(void)viewDidAppear:(BOOL)animated{
    [self getData];
    [super viewDidAppear:animated];
}
-(void)getData{
   
    NSString *userId = [UserDefaultUtil getUserId];
    NSDictionary *dic =@{@"identifier":userId};
    
    HttpReauestParam *param = [[HttpReauestParam alloc]initWithData:@"app_Personal_information_get" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
        
        if ([data[@"success"] boolValue]) {
            
            data = data[@"result"];
            [UserDefaultUtil saveData:data[@"userPhoto"] key:USER_PHOTO];
            [UserDefaultUtil saveData:data[@"userName"] key:USER_NAME];
            [UserDefaultUtil saveData:data[@"authStatus"] key:USER_STATUS];
            [UserDefaultUtil saveData:data[@"walletBalanc"] key:PEOPLE_LAB1];
            [UserDefaultUtil saveData:data[@"gainsMonth"] key:PEOPLE_LAB2];
            [UserDefaultUtil saveData:data[@"orderNum"] key:PEOPLE_LAB3];
            [UserDefaultUtil saveData:data[@"netName"] key:USER_CARTEAM];
            
            [self setUserView];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

-(NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
- (IBAction)search_click:(id)sender {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
    
    
    SearchOrderViewController *searchOrderViewController = [story instantiateViewControllerWithIdentifier:@"search_order_view"];
    
    [self.navigationController pushViewController:searchOrderViewController animated:YES];
}
@end
